package com.example.muasic.activity;

import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.muasic.R;
import com.example.muasic.fragment.PlayQueueFragment;
import com.example.muasic.fragment.QuickControlsFragment;
import com.example.muasic.info.Info;
import com.example.muasic.provider.MusicSqLiteHelper;
import com.example.muasic.service.IService;
import com.example.muasic.service.MusicService;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class RecMusicActivity extends AppCompatActivity {

    private IService iService;
    private ListView listView;
    private static ArrayAdapter<String> adapter;
    private ServiceConnection conn;
    private ImageView Sort;
    private ImageView search;
    private MusicSqLiteHelper musicSqLiteHelper;
    private String TAG = "PlayMusicActivity";
    private static List<String> titleList = new ArrayList<>();
    private static List<Info> musicList = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_music);
        musicSqLiteHelper = new MusicSqLiteHelper(this,"music.db",1);
        Bundle buddle = this.getIntent().getExtras();
        titleList=musicSqLiteHelper.RecmusicName(musicSqLiteHelper);
        musicList =musicSqLiteHelper.RecmusicList(musicSqLiteHelper);
        Sort=findViewById(R.id.iv_play_sort);
        listView = (ListView)findViewById(R.id.music_play_lv);
        search=findViewById(R.id.iv_search);

        titleList=musicSqLiteHelper.RecmusicName(musicSqLiteHelper);
        musicList =musicSqLiteHelper.RecmusicList(musicSqLiteHelper);

        listView = (ListView)findViewById(R.id.music_play_lv);


        // 开启服务
        Intent intent = new Intent(this, MusicService.class);
        startService(intent);
        conn = new ServiceConnection(){
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                //6. 获取IBinder对象, 以此调用暴露的函数
                iService = (IService) service;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
            }
        };

        //绑定服务

        bindService(intent, conn, BIND_AUTO_CREATE);


        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,titleList);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(iService.callgetNowMusic()<0||iService.callgetNowMusic()>=musicList.size()) {
                    PlayingActivity.playing(musicList.get(position).getData(), iService);

                }else {
                    long nowMusicId=iService.callgetNowMusicId();
                    if (nowMusicId != musicList.get(position).getSongId()) {
                        PlayingActivity.playing(musicList.get(position).getData(), iService);
                        //iService.callgetNowmusicList(musicList);
                    }
                }
                iService.callNowMusic(position);
                QuickControlsFragment.setPosition(position);
                QuickControlsFragment.setmusicList(musicList);
                PlayQueueFragment.setmusicList(musicList);
                QuickControlsFragment.updateNowplayingCard();
                Intent intent = new Intent(RecMusicActivity.this,
                        PlayingActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt("music_send",position);
                bundle.putSerializable("musiclist", (Serializable) musicList);
                intent.putExtras(bundle);
                startActivityForResult(intent,position);
             /*   startActivity(intent);*/

            }
        });
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RecMusicActivity.this,
                        SearchActivity.class);
                startActivity(intent);
            }
        });
        Sort.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {

                new AlertDialog.Builder(RecMusicActivity.this).setTitle("选择排序方式").
                        setItems(R.array.str_body, new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, final int which) {
                                String[] aryshop=getResources().getStringArray(R.array.str_body);
                                new AlertDialog.Builder(RecMusicActivity.this)
                                        .setMessage(aryshop[which])
                                        .setNegativeButton("ok", new DialogInterface.OnClickListener() {

                                            @Override
                                            public void onClick(DialogInterface arg0, int arg1) {
                                                switch (which){
                                                    case 0://时间
                                                        musicList=musicSqLiteHelper.TimeLocalmusic2(musicSqLiteHelper);
                                                        titleList=musicSqLiteHelper.TimeLocalmusicName2(musicSqLiteHelper);
                                                        update();
                                                        break;
                                                    case 1://字母
                                                        musicList=musicSqLiteHelper.Alphabetmusic2(musicSqLiteHelper);
                                                        titleList=musicSqLiteHelper.AlphabetmusicName2(musicSqLiteHelper);
                                                        update();
                                                        break;
                                                    case 2://作者
                                                        musicList=musicSqLiteHelper.ArtistLocalmusic2(musicSqLiteHelper);
                                                        titleList=musicSqLiteHelper.ArtistLocalmusicName2(musicSqLiteHelper);
                                                        update();
                                                        break;
                                                }
                                            }
                                        }).show();
                            }
                        }).show();
            }});

    }
    private void update(){
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,titleList);
        listView.setAdapter(adapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        titleList=musicSqLiteHelper.RecmusicName(musicSqLiteHelper);
        musicList =musicSqLiteHelper.RecmusicList(musicSqLiteHelper);
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,titleList);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }




    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 在activity结束的时候回收资源
        /* iService.callDestroyMusic();*/
        unbindService(conn);
    }


}
